package com.yangkunhong.dao;

import com.yangkunhong.pojo.User;
import com.yangkunhong.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class UserDao extends BaseDao{

    public static boolean addRegisterUser(User user){
        String insertSQL = "INSERT INTO user(username,password,createAt,createIp)VALUES(?,?,?,?)";
        boolean insert = UserDao.insert(insertSQL, new Object[]{
                user.getUsername(),
                user.getPassword(),
                user.getCreateAt(),
                user.getCreateIp()
        });
        return insert;
    }
    //根据用户名获取用户信息，用于 登录
    public static User findUserByUsername(User loginUser){
        //第一步获取数据连接池的资源
        DataSource dataSource = DBUtils.getDataSource();
        //第二步创建QueryRunner的查询构造器
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //第三步 编写SQL
        //如果直接带入参数值，容易造成SQL注入
        //用?占位符表示参数，有几个参数得几个问号
        //用?占位符表示参数，有几个参数得几个问号
        //用?占位符表示参数，有几个参数得几个问号
        String selectSQL = "SELECT * FROM user WHERE username=? ";
        //第四步 调用对应的API
        User user = null;
        try {
             //new BeanHandler<>(User.class) 表示查询结果自动封装为单个User对象实例
             user = queryRunner.query(selectSQL, new BeanHandler<>(User.class), loginUser.getUsername());
        } catch (SQLException throwables) {
            System.out.println("根据用户名查询失败");
            throwables.printStackTrace();
        }
        return  user;

    }

    public static List<User> findUserListByUsername(User searchUser,int pageNum,int pageSize){
        //第一步获取数据连接池的资源
        DataSource dataSource = DBUtils.getDataSource();
        //第二步创建QueryRunner的查询构造器
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //第三步 编写SQL
        //如果直接带入参数值，容易造成SQL注入
        //用?占位符表示参数，有几个参数得几个问号
        //用?占位符表示参数，有几个参数得几个问号
        //用?占位符表示参数，有几个参数得几个问号
        String selectSQL = "SELECT * FROM user WHERE username LIKE ? LIMIT ?,? ";
        List<User> userList = null;

        try {
             userList = queryRunner.query(selectSQL, new BeanListHandler<>(User.class), searchUser.getUsername() + "%", (pageNum - 1) * pageSize, pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return userList;
    }


    //统计用户表的记录数

    public static Long getUserCount(User user){
        //第一步获取数据连接池的资源
        DataSource dataSource = DBUtils.getDataSource();
        //第二步创建QueryRunner的查询构造器
        QueryRunner queryRunner = new QueryRunner(dataSource);
        //第三步 编写SQL
        //如果直接带入参数值，容易造成SQL注入
        //用?占位符表示参数，有几个参数得几个问号
        //用?占位符表示参数，有几个参数得几个问号
        //用?占位符表示参数，有几个参数得几个问号
        String selectSQL = "SELECT count(1) FROM user ";
        Long integer = 0L;
        try {
             integer = queryRunner.query(selectSQL, new ScalarHandler<>());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return integer;

    }
    public static int updateAfterLogin(User user){
        DataSource dataSource = DBUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String updateSQL = "UPDATE user SET lastAt=? ,lastIp=? WHERE id=?";
        int update = 0;
        try {
            update = queryRunner.update(updateSQL, user.getLastAt(),user.getLastIp(), user.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }
    public static int updatePassword(User user){
        DataSource dataSource = DBUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String updateSQL = "UPDATE user SET password=? WHERE id=?";
        int update = 0;
        try {
             update = queryRunner.update(updateSQL, user.getPassword(), user.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }

    public static int deleteUserById(User user){
        DataSource dataSource = DBUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String updateSQL = "DELETE FROM user  WHERE id=?";
        int update = 0;
        try {
            update = queryRunner.update(updateSQL,user.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return update;
    }






}
